package finance

import grails.test.*
import org.apache.commons.math.random.RandomDataImpl

class WaldWolfowitzServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }
    
    protected void tearDown() {
        super.tearDown()
    }
    
    def waldWolfowitzService
    
    def list = [1,-1,-1,-1,1,1,1,0,1,1,-1,-1,-1,1,0,-1,1,-1,0,1]
    
    void testRuns() {
        def sublist = [1, -1, -1, -1]
        def runs = waldWolfowitzService.totalRuns(sublist)
        assertEquals 2, runs
        
        sublist = [1, -1, -1, -1, 1, 1, 1, 0, 1, 1]
        runs = waldWolfowitzService.totalRuns(sublist)	
        assertEquals 4, runs
        
        runs = waldWolfowitzService.totalRuns(list)	
        assertEquals 10, runs
    }
    
    void testNeg() {
        def minuses = waldWolfowitzService.neg(list)
        assertEquals 8, minuses
    }
    
    void testPos() {
        def pluses = waldWolfowitzService.pos(list)
        assertEquals 9, pluses
    }
    
    void testZ() {
        def uniform = []
        def rdi = new Random()
        
        for( i in 0..255)
            uniform << rdi.nextDouble()
        
        def z = waldWolfowitzService.Z(uniform)
        println z
        assertTrue z > 1.645
    }
    
    void testExpectedRuns() {
        def runs = waldWolfowitzService.expectedRuns(8, 9)	
        assertEquals 9.47, runs, 0.01
    }
    
    void testStddev() {
        def sigma = waldWolfowitzService.stddev(8, 9)
        assertEquals 1.99, sigma, 0.01
    }
}
